<?php
/**
 * Created by JetBrains PhpStorm.
 * User: MeRo
 * Date: 15.09.13
 * Time: 18:46
 * To change this template use File | Settings | File Templates.
 */

require_once 'templates/train/lib.php';
class newSurfaceValidation
{
    protected $_contao = NULL;

    protected $_errors = array();

    protected $_fields = array(
        array('name' => 'surface_name',        'length' => 255,    'mandatory' => true),
        array('name' => 'surface_x_size',      'length' => 4,      'mandatory' => true),
        array('name' => 'surface_y_size',      'length' => 4,      'mandatory' => true),
        array('name' => 'surface_public',      'length' => false,  'mandatory' => false),
        array('name' => 'surface_description', 'length' => false,  'mandatory' => false)
    );

    protected $_errorFields = array();

    function __construct($contao) {
        $this->_contao = $contao;

        $this
            ->_validateMandatories()
            ->_validateFieldLength()
            ->_returnErrors()
            ->_dataEntry()
        ;
    }

    protected function _validateMandatories() {
        $errorFields = array();

        foreach ($this->_fields as $field) {
            if ($field['mandatory'] === true &&
            (get($field['name']) === '' || get($field['name']) === 0)) {
                $errorFields[$field['name']] = true;
            }
        }

        if (count($errorFields) > 0) {
            $this->_errors[] = 'Bitte f&uuml;lle alle markierten Felder aus.';
            $this->_errorFields = array_merge($this->_errorFields, $errorFields);
        }

        return $this;
    }

    protected function _validateFieldLength() {
        $errorFields = array();

        foreach ($this->_fields as $field) {
            if ($field['length'] != false && strlen(get($field['name'])) > $field['length']) {
                $errorFields[$field['name']] = true;
            }
        }

        if (count($errorFields) > 0) {
            $this->_errors[] = 'Der Inhalt der markierten Felder ist zu lang.';
            $this->_errorFields = array_merge($this->_errorFields, $errorFields);
        }

        return $this;
    }

    protected function _returnErrors() {
        if (array_filter($this->_errors) != array()) {
            $errors = array('fields' => $this->_errorFields, 'errors' => $this->_errors);
            echo json_encode($errors);
            die();
        }

        return $this;
    }

    protected function _dataEntry() {
        $memberId = NULL;
        if (FE_USER_LOGGED_IN) {
            $memberId = $this->_contao->Member->id;
        }

        $insert = array(
            'surface_name'         => get('surface_name'),
            'surface_x_size'       => get('surface_x_size'),
            'surface_y_size'       => get('surface_y_size'),
            'surface_public'       => get('surface_public'),
            'surface_description'  => get('surface_description'),
            'surface_create_user'  => $memberId
        );
        $surfaceId = $this->_contao->Database->prepare("INSERT INTO tr_surface %s")->set($insert)->execute()->insertId;
        echo json_encode(array(
            'id'                    => $surfaceId,
            'surface_name'         => get('surface_name'),
            'surface_x_size'       => get('surface_x_size'),
            'surface_y_size'       => get('surface_y_size'),
            'surface_public'       => get('surface_public'),
            'surface_description'  => get('surface_description'),
            'surface_data_json'    => NULL,
            'surface_create_user'  => $memberId,
            'surface_create_date'  => date('Y-m-d H:i:s'),
            'surface_update_date'  => NULL,
            'surface_update_user'  => NULL
        ));
        die();
    }
}

$this->import('Database');
$this->import('FrontendUser', 'Member');
new newSurfaceValidation($this);
